package com.hzncc.flowable_diboot.rabbitmq.service.impl;

import com.diboot.core.binding.QueryBuilder;
import com.diboot.core.util.JSON;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.entity.IamUser;
import com.diboot.iam.service.IamUserService;
import com.diboot.notification.entity.Message;
import com.diboot.notification.service.MessageService;
import com.hzncc.flowable_diboot.dto.MessageRecordDTO;
import com.hzncc.flowable_diboot.entity.MessageRecord;
import com.hzncc.flowable_diboot.rabbitmq.config.Cons;
import com.hzncc.flowable_diboot.rabbitmq.service.MessageQueueService;
import com.hzncc.flowable_diboot.service.MessageRecordService;
import com.hzncc.flowable_diboot.vo.MessageRecordListVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 消息发送
 *
 * @author 陈广煜
 * @date 2024/5/20
 */
@Service
@RequiredArgsConstructor
public class SendMessageService {

    private final MessageQueueService messageQueueService;
    private final MessageRecordService messageRecordService;
    private final IamUserService iamUserService;
    private final MessageService messageService;

    /**
     * 消息发送
     *
     * @param content 消息内容
     * @author 陈广煜
     * @date 2024/5/20
     */
    @Deprecated
    public void sendRetainedMessage(MessageRecord content) {
        String topic = getKey(content);
        messageRecordService.createEntity(content);
        // 未接收消息
        List<MessageRecordListVO> msgs = messageRecordService.getViewObjectList(QueryBuilder.toQueryWrapper(new MessageRecordDTO().setReceiver(content.getReceiver()).setIsView(false)), new Pagination(), MessageRecordListVO.class);
        // 保留消息
        messageQueueService.sendRetainedMessage(topic, JSON.stringify(msgs));
    }

    /**
     * 消息发送
     *
     * @param content 消息内容
     * @author 陈广煜
     * @date 2024/5/20
     */
    public void sendRetainedMessage(Message content) {
        String topic = getKey(content);
        content.setStatus(com.diboot.notification.config.Cons.MESSAGE_STATUS.DELIVERY.name());
        messageService.createOrUpdateEntity(content);
        // 保留消息
        messageQueueService.sendRetainedMessage(topic, "您有一条新信息");
    }

    private String getKey(Message msg) {
        String uid = msg.getReceiver();
        if (StringUtils.isBlank(uid)) {
            throw new RuntimeException("接收人不能为空");
        }
        IamUser user = iamUserService.getEntity(uid); // 接收人
        String topic = user.getTenantId() + "-" + user.getUserNum();
        return topic;
    }

    /**
     * 消息发送
     *
     * @param uid 接收人
     * @param msg 消息内容
     * @author 陈广煜
     * @date 2024/5/29
     */
    public void sendRetainedMessage(String uid, String msg) {
        IamUser user = iamUserService.getEntity(uid); // 接收人
        String topic = user.getTenantId() + "-" + user.getUserNum();
        messageQueueService.sendRetainedMessage(topic, msg);
    }

    /**
     * @param uid     接收人
     * @param topic   消息队列topic
     * @param content 消息内容
     * @author 陈广煜
     * @date 2024/5/20
     */
    @Deprecated
    public void sendMessage(String uid, String topic, String content) {
        // todo 判断用户是否在线
        boolean isOnline = true;
        if (isOnline) {
            // 发送消息
            messageQueueService.sendMessage(topic, content);
        }
        // 消息记录
        MessageRecord record = new MessageRecord();
        record.setIsSend(true).setIsView(true).setMessageContent(content).setReceiver(uid);
        messageRecordService.createEntity(record);
    }


    /**
     * 发送延时消息
     *
     * @param record 消息
     * @param delay  延时时间
     * @author 陈广煜
     * @date 2024/6/17
     */
    public void sendDelayMessage(Message record, int delay) {
        //messageService.createEntity(record);
        messageQueueService.sendDelayedMessageDirect(Cons.DELAY_EXCHANGE, Cons.ROUTING_KEY, JSON.stringify(record), delay);
    }

    ;

    /**
     * 获取key
     *
     * @param record
     * @return
     * @author 陈广煜
     * @date 2024/6/17
     */
    private String getKey(MessageRecord record) {
        String uid = record.getReceiver();
        if (StringUtils.isBlank(uid)) {
            throw new RuntimeException("接收人不能为空");
        }
        IamUser user = iamUserService.getEntity(uid); // 接收人
        String topic = user.getTenantId() + "-" + user.getUserNum();
        return topic;
    }
}
